/**
 * 
 */
package com.xcn.socket;

import java.io.*;
import java.net.*;

import kr.co.xcurenet.crypt.*;

import com.xcn.exception.*;
import com.xcn.runtime.*;


/**
 * @author changmin
 *
 */
public class Daemon extends Thread
{
	private static ServerSocket _server = null;
	
	/**
	 * 
	 */
	public Daemon( )
	{
	}
	
	public void accept( ) throws IOException
	{
		try
		{
			while ( true )
			{
				Socket socket = _server.accept ( );
				System.out.println ( "Accept..." );
				if ( socket != null )
				{
					DataInputStream dis = new DataInputStream ( new BufferedInputStream ( socket.getInputStream ( ) ) );
					DataOutputStream dos = new DataOutputStream ( new BufferedOutputStream ( socket.getOutputStream ( ) ) );
					if ( dis != null )
					{
						String message = dis.readUTF ( );
						if ( message != null )
						{
							sendMsg ( dos, message  );
						}
					}
					dis.close ( );
					dos.close ( );
				}
				socket.close ( );
			}
		}
		catch ( IOException e )
		{
			e.printStackTrace ( );
		}
		finally
		{
			close( );
		}
	}
	
	
	public void cipherMsg( String message, DataOutputStream out )
	{
		System.out.println ( message );

		byte[] key = new byte[] {'7', 'T', 'Q', 'F', '1', 'N', '4', 'U', 'K', 'X', 'O', 'K', 'D', 'N', 'N', 'G'};
		try
		{
			ARIA _aria = new ARIA ( key );
			_aria.encode ( new ByteArrayInputStream ( message.getBytes ( ) ), out, message.getBytes ( ).length );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		finally
		{
			try { out.flush ( ); } catch ( Exception e2 ) {}
			try { out.close ( ); } catch ( Exception e2 ) {}
		}
	}
	

	public void sendMsg ( DataOutputStream out, String message  ) throws IOException
	{
		RuntimeExe runtime = new RuntimeExe ( );
		try
		{
			cipherMsg ( runtime.getRuntime ( message ), out );
		}
		catch ( XCNException e )
		{
			e.printStackTrace ( );
		}
	}
	
	
	public void start( )
	{
		if ( _server == null || _server.isClosed ( ) )
		{
			try
			{
				_server = new ServerSocket ( 7777 );

				accept ( );
			}
			catch ( IOException e )
			{
				e.printStackTrace ( );
			}
		}
		else
		{
			System.out.println ( "daemon server is running.." );
		}
	}

	public void close( ) throws IOException
	{
		if ( _server != null && !_server.isClosed ( ) )
		{
			_server.close ( );
			_server = null;
		}
		System.out.println ( "close daemon server...." );
	}

	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main ( String[] args ) throws IOException
	{
		if ( args.length == 0 )
		{
			System.err.println ( "Daemon server error execute args.. ex:(start or stop).." );
			System.exit ( 0 );
		}

		Daemon _daemon = new Daemon ( );
		if ( args[0].equals ( "stop" ) )
		{
			if ( _server != null && !_server.isClosed ( ) )
				_server.close ( );
		}
		else if ( args[0].equals ( "start" ) )
		{
			_daemon.start ( );
		}
		else
		{
			System.err.println ( "Daemon server error execute args.. ex:(start or stop).." );
			System.exit ( 0 );
		}
	}
}
